package com.gitee.qdbp.general.common.biz.message.service;

import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.general.common.api.message.model.SystemMessageBean;
import com.gitee.qdbp.general.common.api.message.model.SystemMessageUpdate;
import com.gitee.qdbp.general.common.api.message.service.ISystemMessageExecutor;
import com.gitee.qdbp.general.common.biz.message.basic.SystemMessageBasic;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 系统消息业务处理类
 *
 * @author zhh
 * @version 170802
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class SystemMessageExecutor implements ISystemMessageExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(SystemMessageExecutor.class);

    /** 系统消息DAO **/
    @Autowired
    private SystemMessageBasic systemMessageBasic;

    @Override
    public void create(SystemMessageBean model, IAccount me) throws ServiceException {
        String msg = "Failed to create SystemMessage. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // TODO: 参数验证
        // if (VerifyTools.isBlank(model.getXxx())) {
        //     log.error(msg + "params is null: xxx");
        //     throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        // }

        // 业务逻辑
        model.setId(null); // 主键由Basic生成, 不允许外部传入
        // TODO: 补充其他业务逻辑
        // 设置创建人信息
        // model.setCreatorId(me.getUserId()); // 创建人ID
        // model.setCreatorName(me.getDisplayName()); // 创建人姓名

        // 向gn_system_message表插入记录
        systemMessageBasic.create(model);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void update(SystemMessageUpdate model, IAccount me) throws ServiceException {
        String msg = "Failed to update SystemMessage. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // TODO: 参数验证
        // if (VerifyTools.isBlank(model.getXxx())) {
        //     log.error(msg + "params is null: Xxx");
        //     throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        // }

        String id = model.getId();
        if (VerifyTools.isBlank(id) && model.getWhere() != null) {
            id = model.getWhere().getId();
        }
        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 原始记录检查
        SystemMessageBean older = systemMessageBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found. id=" + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        // TODO: 业务逻辑
        // 判断哪些状态的记录不允许修改...
        // if (VerifyTools.isExists(older.getState(), State.DISABLED, State.Xxx)) {
        //     throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        // }

        SystemMessageUpdate newer = createChangedModel(model, older);

        if (newer != null) {
            // 更新gn_system_message表的记录
            systemMessageBasic.update(newer, false);
        }

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void delete(String id, IAccount me) throws ServiceException {
        String msg = "Failed to delete SystemMessage. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 从gn_system_message表查询原记录
        SystemMessageBean older = systemMessageBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found.\n\tid is " + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        // TODO: 业务逻辑
        // 判断哪些状态的记录不允许删除...
        // if (VerifyTools.isExists(older.getState(), State.DISABLED, State.Xxx)) {
        //     throw new ServiceException(ResultCode.RECORD_STATE_ERROR);
        // }

        // 删除gn_system_message表的记录
        systemMessageBasic.deleteByIds(Arrays.asList(id), false);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    @Override
    public void delete(List<String> ids, IAccount me) throws ServiceException {
        String msg = "Failed to delete SystemMessage. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除gn_system_message表的记录
        systemMessageBasic.deleteByIds(ids, false);

        throw new UnsupportedOperationException("该方法的业务逻辑暂未实现"); // TODO 完成业务逻辑的实现之后删除此行
    }

    /**
     * 创建一个新的对象, 只包含改动过的字段
     *
     * @author zhh
     * @param model 目标对象(一般是参数传入的)
     * @param older 源对象(一般是从数据库查询得到的)
     * @return 只包含有更新的字段对象
     */
    private SystemMessageUpdate createChangedModel(SystemMessageUpdate model, SystemMessageBean older) {
        // SystemMessageUpdate newer = new SystemMessageUpdate();
        // newer.setWhere(model.getWhere());
        //
        // boolean changed = false;
        // newer.setId(model.getId()); // 消息ID
        //
        // if (VerifyTools.isChanged(model.getType(), older.getType())) {
        //     changed = true;
        //     newer.setType(model.getType()); // 消息类型(0.通知)
        // }
        // if (VerifyTools.isChanged(model.getGroupId(), older.getGroupId())) {
        //     changed = true;
        //     newer.setGroupId(model.getGroupId()); // 群组ID(两人对话时以双方的ID字符连接作为群组ID)
        // }
        // if (VerifyTools.isChanged(model.getSenderId(), older.getSenderId())) {
        //     changed = true;
        //     newer.setSenderId(model.getSenderId()); // 发送人ID
        // }
        // if (VerifyTools.isChanged(model.getSendTime(), older.getSendTime())) {
        //     changed = true;
        //     newer.setSendTime(model.getSendTime()); // 发送时间
        // }
        // if (VerifyTools.isChanged(model.getReceiverId(), older.getReceiverId())) {
        //     changed = true;
        //     newer.setReceiverId(model.getReceiverId()); // 接收人ID
        // }
        // if (VerifyTools.isChanged(model.getTitle(), older.getTitle())) {
        //     changed = true;
        //     newer.setTitle(model.getTitle()); // 标题
        // }
        // if (VerifyTools.isChanged(model.getContent(), older.getContent())) {
        //     changed = true;
        //     newer.setContent(model.getContent()); // 内容
        // }
        // if (VerifyTools.isChanged(model.getReadTime(), older.getReadTime())) {
        //     changed = true;
        //     newer.setReadTime(model.getReadTime()); // 读取时间
        // }
        // if (VerifyTools.isChanged(model.getReadState(), older.getReadState())) {
        //     changed = true;
        //     newer.setReadState(model.getReadState()); // 状态(0.未读|1.已读)
        // }
        // if (VerifyTools.isChanged(model.getOptions(), older.getOptions())) {
        //     changed = true;
        //     newer.setOptions(model.getOptions()); // 选项
        // }
        // if (VerifyTools.isChanged(model.getDataState(), older.getDataState())) {
        //     changed = true;
        //     newer.setDataState(model.getDataState()); // 数据状态:0为正常|其他为删除
        // }
        // if (Boolean.TRUE.equals(model.isTypeToNull())) {
        //     changed = true;
        //     newer.setTypeToNull(true); // 消息类型(0.通知)更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isGroupIdToNull())) {
        //     changed = true;
        //     newer.setGroupIdToNull(true); // 群组ID(两人对话时以双方的ID字符连接作为群组ID)更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isSenderIdToNull())) {
        //     changed = true;
        //     newer.setSenderIdToNull(true); // 发送人ID更新为空值
        // }
        // if (VerifyTools.isNotBlank(model.getSenderIdAdd()) && model.getSenderIdAdd() != 0) {
        //     changed = true;
        //     newer.setSenderIdAdd(model.getSenderId()); // 发送人ID的增加值
        // }
        // if (Boolean.TRUE.equals(model.isSendTimeToNull())) {
        //     changed = true;
        //     newer.setSendTimeToNull(true); // 发送时间更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isSendTimeToCurrent())) {
        //     changed = true;
        //     newer.setSendTimeToCurrent(true); // 发送时间更新为数据库当前时间
        // }
        // if (VerifyTools.isNotBlank(model.getSendTimeAdd()) && model.getSendTimeAdd() != 0) {
        //     changed = true;
        //     newer.setSendTimeAdd(model.getSendTime()); // 发送时间的增加值
        // }
        // if (Boolean.TRUE.equals(model.isReceiverIdToNull())) {
        //     changed = true;
        //     newer.setReceiverIdToNull(true); // 接收人ID更新为空值
        // }
        // if (VerifyTools.isNotBlank(model.getReceiverIdAdd()) && model.getReceiverIdAdd() != 0) {
        //     changed = true;
        //     newer.setReceiverIdAdd(model.getReceiverId()); // 接收人ID的增加值
        // }
        // if (Boolean.TRUE.equals(model.isTitleToNull())) {
        //     changed = true;
        //     newer.setTitleToNull(true); // 标题更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isContentToNull())) {
        //     changed = true;
        //     newer.setContentToNull(true); // 内容更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isReadTimeToNull())) {
        //     changed = true;
        //     newer.setReadTimeToNull(true); // 读取时间更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isReadTimeToCurrent())) {
        //     changed = true;
        //     newer.setReadTimeToCurrent(true); // 读取时间更新为数据库当前时间
        // }
        // if (VerifyTools.isNotBlank(model.getReadTimeAdd()) && model.getReadTimeAdd() != 0) {
        //     changed = true;
        //     newer.setReadTimeAdd(model.getReadTime()); // 读取时间的增加值
        // }
        // if (Boolean.TRUE.equals(model.isReadStateToNull())) {
        //     changed = true;
        //     newer.setReadStateToNull(true); // 状态(0.未读|1.已读)更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isOptionsToNull())) {
        //     changed = true;
        //     newer.setOptionsToNull(true); // 选项更新为空值
        // }
        // return changed ? newer : null;

        throw new UnsupportedOperationException("请确认SystemMessageUpdate哪些字段是允许修改的"); // TODO 完成业务逻辑的实现之后删除此行
    }

}